import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import autoprefixer from 'autoprefixer'
import postcssPixelToRemPlugin from 'postcss-pxtorem'
import compression from 'vite-plugin-compression'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vitePluginBundleObfuscator from 'vite-plugin-bundle-obfuscator'
import path from 'path'
export default defineConfig({
    base: '', // 使用相对路径
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src')
        }
    },
    server: {
        proxy: {
            '/req': {
                // target: 'https://airedale-immense-badly.ngrok-free.app/', // 目标服务器的地址
                target: 'http://localhost:8050', // 目标服务器的地址
                changeOrigin: true,
                rewrite: path => path.replace(/^\/req/, '') // 可选：重写路径
            }
        }
    },
    build: {
        assetsInlineLimit: 10240 // 10kb
        // minify: 'esbuild' // 默认
    },
    esbuild: {
        drop: ['console', 'debugger'] //打包去除
    },
    plugins: [
        Vue(),
        AutoImport({
            imports: ['vue'],
            resolvers: [
                ElementPlusResolver(),
                IconsResolver({
                    prefix: 'Icon'
                })
            ]
        }),
        Components({
            resolvers: [
                IconsResolver({
                    enabledCollections: ['ep']
                }),
                ElementPlusResolver()
            ]
        }),
        Icons({
            autoInstall: true
        }),
        compression({
            algorithm: 'gzip', // 使用gzip压缩
            threshold: 10240, // 文件大小超过10KB时进行压缩
            deleteOriginFile: false // 是否删除原文件
        }),
        vitePluginBundleObfuscator({
            enable: true,
            log: true,
            autoExcludeNodeModules: true,
            threadPool: true
        })
    ],
    css: {
        postcss: {
            plugins: [
                autoprefixer(),
                postcssPixelToRemPlugin({
                    rootValue: 16,
                    propList: ['*'],
                    selectorBlackList: ['ignore-'],
                    replace: true,
                    mediaQuery: false,
                    minPixelValue: 0
                })
            ]
        }
    }
})
